﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
using System.Text.RegularExpressions;


namespace BaHuan
{
    public partial class FrmKhachHang : Form
    {
        private KhachHang khachhang = new KhachHang();
        private KhachHangManagement khachhangMgt = new KhachHangManagement();

        private bool IsEdited = false;
        public FrmChoCuocGoi parentForm;

        public FrmKhachHang()
        {
            InitializeComponent();
        }

        public void BindingToGrid()
        {
            ResultObj result = new ResultObj();
            result = khachhangMgt.GetAllKhachHang();
            List<KhachHang> lstKhachHang = (List<KhachHang>)result.Result;
            dgvKhachhang.DataSource = lstKhachHang;
        }

        public void ChangeStatusTextBox(bool IsReadOnly)
        {
            if (IsReadOnly)
            {
                txtTenKH.ReadOnly = true;
                txtDiaChi.ReadOnly = true;
                txtMaSoThue.ReadOnly = true;
                txtChietKhau.ReadOnly = true;
                txtWebsite.ReadOnly = true;
                txtEmail.ReadOnly = true;
                txtFax.ReadOnly = true;
                txtDienThoai.ReadOnly = true;
                txtMaSoKhachHang.ReadOnly = true;

            }
            else
            {
                txtTenKH.ReadOnly = false;
                txtDiaChi.ReadOnly = false;
                txtMaSoThue.ReadOnly = false;
                txtChietKhau.ReadOnly = false;
                txtWebsite.ReadOnly = false;
                txtEmail.ReadOnly = false;
                txtFax.ReadOnly = false;
                txtDienThoai.ReadOnly = false;
                txtMaSoKhachHang.ReadOnly = false;
            }
        }

        public void clearTextbox()
        {
            txtTenKH.Clear();
            txtDiaChi.Clear();
            txtChietKhau.Clear();
            txtMaSoThue.Clear();
            txtWebsite.Clear();
            txtEmail.Clear();
            txtFax.Clear();
            txtDienThoai.Clear();
            txtMaSoKhachHang.Clear();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            btnClose.Focus();
        }

        private void btnThemKH_Click(object sender, EventArgs e)
        {
            btnSua.Enabled = false;
            btnLuu.Enabled = true;
            clearTextbox();
            grbKhachhang.Enabled = true;
            ChangeStatusTextBox(false);
            IsEdited = false;
            txtTenKH.Select();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtTenKH.Text == "" )
            {
                MessageBox.Show("Bạn Chưa nhập tên khách hàng !!!");
                return;
            }
            else
            {
                ResultObj result = new ResultObj();

                string sTen = txtTenKH.Text;
                string sMaSoThue = txtMaSoThue.Text;
                string sDiaChi = txtDiaChi.Text;
                string sDienThoai = txtDienThoai.Text;
                string sFax = txtWebsite.Text;
                string sEmail = txtEmail.Text;
                string sWebsite = txtFax.Text;
                string sMaSoKhachHang = txtMaSoKhachHang.Text;
                double dChietKhau = txtChietKhau.Text == "" ? 0 : Convert.ToDouble(txtChietKhau.Text);

                KhachHang khachhang = new KhachHang();
                khachhang.Ten = sTen;
                khachhang.MaSoThue = sMaSoThue;
                khachhang.DiaChi = sDiaChi;
                khachhang.DienThoai = sDienThoai;
                khachhang.Fax = sFax;
                khachhang.Email = sEmail;
                khachhang.Website = sWebsite;
                khachhang.ChietKhau = dChietKhau;
                khachhang.MaSoKhachHang = sMaSoKhachHang;

                if (!IsEdited)
                {
                    result = khachhangMgt.SaveNewKhachHang(khachhang);
                    khachhang.Id = result.ReturnCode;
                }
                else
                {
                    khachhang.Id = Convert.ToInt32(lblId.Text);
                    result = khachhangMgt.UpdateKhachHang(khachhang);
                    IsEdited = false;
                }

                if (result.ReturnCode > 0)
                {
                    MessageBox.Show("Lưu thành công", "Thông báo");
                    grbKhachhang.Enabled = false;
                    btnLuu.Enabled = false;
                    if (!IsEdited && parentForm != null)
                    {
                        FrmPhieuXuatHang frm = new FrmPhieuXuatHang();
                        frm.NhanVien = parentForm.nhanvien;
                        parentForm.khachhang = khachhang;
                        frm.parentFrmChoCuocGoi = parentForm;
                        this.Hide();
                        frm.ShowDialog();
                        this.Close();
                    }
                    else
                        BindingToGrid();
                }
                else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                else
                    MessageBox.Show("Thất bại");
            }
        }

        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            lblId.Hide();
            grbKhachhang.Enabled = false;
            btnLuu.Enabled = false;
            btnSua.Enabled = false;
            BindingToGrid();
        }

        private void btnThemChinhanhKH_Click(object sender, EventArgs e)
        {
            if (dgvKhachhang.CurrentRow != null)
            {
                KhachHang khachhang = (KhachHang)dgvKhachhang.CurrentRow.DataBoundItem;
                FrmChiNhanhKhachHang frm = new FrmChiNhanhKhachHang();
                frm.KhachHang = khachhang;
                frm.Show();
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = true;
            IsEdited = true;
            grbKhachhang.Enabled = true;
            ChangeStatusTextBox(false);
            txtTenKH.Select();
        }

        private void dgvKhachhang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                btnSua.Enabled = true;
                btnLuu.Enabled = false;
                ChangeStatusTextBox(true);
                lblId.Text = dgvKhachhang.Rows[e.RowIndex].Cells[0].Value.ToString();
                txtTenKH.Text = dgvKhachhang.Rows[e.RowIndex].Cells[2].Value.ToString();
                txtDiaChi.Text = dgvKhachhang.Rows[e.RowIndex].Cells[4].Value.ToString();
                txtMaSoThue.Text = dgvKhachhang.Rows[e.RowIndex].Cells[3].Value.ToString();
                txtChietKhau.Text = dgvKhachhang.Rows[e.RowIndex].Cells[9].Value.ToString();
                txtWebsite.Text = dgvKhachhang.Rows[e.RowIndex].Cells[8].Value.ToString();
                txtEmail.Text = dgvKhachhang.Rows[e.RowIndex].Cells[7].Value.ToString();
                txtFax.Text = dgvKhachhang.Rows[e.RowIndex].Cells[6].Value.ToString();
                txtDienThoai.Text = dgvKhachhang.Rows[e.RowIndex].Cells[5].Value.ToString();
                txtMaSoKhachHang.Text = dgvKhachhang.Rows[e.RowIndex].Cells[1].Value.ToString();
            }
        }

        private bool kiemtranhaplieu1()
        {
            if (FrmKhachHang.KiemTraEmail(txtEmail.Text) == false)
            {
                MessageBox.Show("Email khong hop le", "thong bao");
                return false;
            }
            return true;
        }

        static public bool KiemTraEmail(string email)
        {
            if (email.Length < 1)
                return false;
            try
            {
                int mail = int.Parse(email.Substring(0, 1));
                return false;
            }
            catch
            {
                string dang = @"^[_a-zA-Z0-9][_a-zA-Z0-9]*@[-a-zA-Z0-9]+(\.[_.a-zA-Z0-9]+)*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|vn|[a-zA-Z]{2})$";

                Regex kt = new Regex(dang, RegexOptions.IgnorePatternWhitespace);
                bool co = false;

                if (string.IsNullOrEmpty(email))
                {
                    co = false;
                }
                else
                {
                    co = kt.IsMatch(email.ToLower());
                }

                return co;
            }
        }

        private void txtChieuKhau_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Vui lòng nhập số !");
                e.Handled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FrmTimKiem frm = new FrmTimKiem();
            frm.Show();
        }

        private void txtTenKH_TextChanged(object sender, EventArgs e)
        {
            txtTenKH.Focus();
        }

        private void btnThemKH_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.T)
            {
                btnSua.Enabled = false;
                btnLuu.Enabled = true;
                clearTextbox();
                grbKhachhang.Enabled = true;
                ChangeStatusTextBox(false);
                IsEdited = false;
            }
        }

        private void txtDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Vui lòng nhập số !");
                e.Handled = true;
            }
        }

        private void txtChietKhau_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void txtFax_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Vui lòng nhập số !");
                e.Handled = true;
            }
        }

        private void txtChietKhau_TextChanged_1(object sender, EventArgs e)
        {
            if (txtChietKhau.Text == "")
                txtChietKhau.Text = "0";
        }

        private void txtEmail_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
